
package edu.luc.concordia;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.EventListenerList;

/**
 * Copyright 2008 Project Concordia
 * <p>
 * This file is part of RetailReturnSystem.
 * <p>
 * RetailReturnSystem is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * <hr>
 * This class generates GUI for Receipt function of Retail
 * Check out and Return System. 
 * @author Rashmi Pandey & Piyushaben Desai
 * @version 0.1 2008-10-10
 * 
 */
public class Receipt extends JPanel implements ActionListener,PropertyChangeListener 
{	
	//All Controls Declaration
	private JTextArea receiptTextArea = new JTextArea(15,20);
	private JButton doneButton;
	private JLabel receiptLabel;
	private JFrame frame;
	
	//Declare and Initialize Listeners
	private EventListenerList listener = new EventListenerList();
	private ActionListener forwardListener = new ActionListener()
	{
		@Override
		public void actionPerformed(ActionEvent e) {
		}		
	};
	
	public void addActionListener(ActionListener l)
	{
		listener.add(ActionListener.class, l);
	}
		
	public Receipt(JFrame frame)
	{
		this.frame = frame;
		this.setLayout(new BorderLayout());
		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		//Receipt Label
		receiptLabel = new JLabel ("Receipt:");
		displayPanel.add(receiptLabel);
		
        //Receipt Text Area  
		receiptTextArea.setEditable(false);
		
		
		receiptTextArea.setText(DateUtils.now() + "\n----------------------------------- \n" + 
				"Cash total :\n" +"Total number of items sold :\n" +	
				"Total Sales transaction :\n" + "Total sales transaction since last : \n" +
				"Total number of items returned :\n"  + 		
				"Net tax : \n" + "Net cash :\n" +"\n\n\n THANK YOU !");
		
		//Scroll
		JScrollPane commentScroll = new JScrollPane(receiptTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	    displayPanel.add(commentScroll);
		
		//Button panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		//Done Button
		doneButton = new JButton("DONE");
		doneButton.addActionListener((ActionListener) this);
		buttonPanel.add(doneButton);
		doneButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
			//String scanCode = scanCodeField.getText();
			ActionEvent scanevent = new ActionEvent(receiptTextArea,0,ActionConstants.PRINT_RECEIPT);
			forwardListener.actionPerformed(scanevent);
			}
		});
		
		this.add(displayPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);		
	}
	
	public Receipt() {
		// TODO Auto-generated constructor stub
	}

	public void setText(String str)
	{
		// Set Text area
		 
		receiptTextArea.setText(str);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if (source == doneButton)
		{	
			this.frame.dispose();	
		}
	}
	
	/**
	 * Receiving Events from Translation
	 */
	@SuppressWarnings("unchecked")
	public void propertyChange(PropertyChangeEvent e) {
		doneButton.addActionListener(this);
		
		if(PropertyConstants.FINAL_RECEIPT.equals(e.getPropertyName()))
		{
		
			ArrayList<String> desc = (ArrayList<String>) e.getNewValue();
			receiptTextArea.setText("");
			
			for (String output : desc)
			{
				
				receiptTextArea.append(output+"\n");
			}
					
		} 		
	}
}	
		

